package com.hb.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @title: LocalDateTimeUtil
 * @description: java8时间工具类
 */
public class LocalDateTimeUtil {

	//LocalDateTime转时间戳(东八区)
	public static long getTimestamp(LocalDateTime localDateTime) {
		return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
	}

	//时间戳转LocalDateTime
	public static LocalDateTime timestamToDatetime(long timestamp) {
		Instant instant = Instant.ofEpochMilli(timestamp);
		return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
	}

	//时间戳转LocalDateTime(秒)
	public static LocalDateTime timeSecondToDatetime(Integer timeSecond) {
		return LocalDateTime.ofEpochSecond(timeSecond, 0, ZoneOffset.ofHours(8));
	}


	//获取当前时间时间戳
	public static Long getNowTimestamp() {
		return Instant.now().toEpochMilli();
	}

	//获取当前时间戳（秒）
	public static Integer getNowTimeSecond() {
		Long second = Instant.now().getEpochSecond();
		return Integer.parseInt(second.toString());
	}

	//LocalDate转时间戳
	public static Long timestampFromLocalDate(LocalDate localDate) {
		return localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
	}

	//LocalDate转时间戳(秒)
	public static Integer timeSecondFromLocalDate(LocalDate localDate) {
		Long timestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().getEpochSecond();
		return Integer.parseInt(timestamp.toString());
	}

	//时间戳转LocalDate
	public static LocalDate timestamToDate(long timestamp) {
		return Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
	}

	//时间戳转LocalDateTime(秒)
	public static LocalDate timeSecondToDate(Integer timeSecond) {
		return Instant.ofEpochSecond(timeSecond * 1000L).atZone(ZoneOffset.ofHours(8)).toLocalDate();
	}


	/*============================================================================*/

	//格式化现在时间
	public static String getNowFormat(String pattern) {
		LocalDateTime nowTime = LocalDateTime.now();
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
		return dateTimeFormatter.format(nowTime);
	}

	//格式化时间返回时间戳
	public static Long convertTimeToLong(String time, String pattern) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
		LocalDateTime parse = LocalDateTime.parse(time, formatter);
		return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
	}

	//格式化时间戳返回时间格式
	public static String convertTimeToStr(Long timestamp, String pattern) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
		Instant instant = Instant.ofEpochMilli(timestamp);
		LocalDateTime parse = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
		return formatter.format(parse);
	}

	//==================================================================================


	//获取当天时间开始的时间戳
	public static Long todayStartTimestamp() {
		//开始时间
		LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
		return getTimestamp(todayStart);
	}

	//获取当天时间结束的时间戳
	public static Long todayEndTimestamp() {
		//结束时间
		LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
		return getTimestamp(todayStart);
	}

	//获取时间对应星期几 （星期一~星期天 依次为 1~7）
	public static int getDayOfWeek(String time) {
		return LocalDate.parse(time).getDayOfWeek().getValue();
	}

	//格式化时间并返回当天开始时间戳
	public static Long dayStartTimestamp(String text, String pattern) {
		LocalDateTime dayStartTimeStamp = LocalDateTime.of(LocalDate.parse(text, DateTimeFormatter.ofPattern(pattern)), LocalTime.MIN);
		return getTimestamp(dayStartTimeStamp);
	}

	//格式化时间并返回当天结束时间戳
	public static Long dayEndTimestamp(String text, String pattern) {
		LocalDateTime dayEndTimeStamp = LocalDateTime.of(LocalDate.parse(text, DateTimeFormatter.ofPattern(pattern)), LocalTime.MAX);
		return getTimestamp(dayEndTimeStamp);
	}


	// 返回传入date的前12个月并全设为0
	public static List<Map<String, Integer>> getAllMonthList(LocalDate paramDate) {
		List<Map<String, Integer>> list = new ArrayList<>();
		for (int i = 0; i < 12; i++) {
			Map<String, Integer> map = new HashMap<>();
			LocalDate localDate = paramDate.plusMonths(-i);
			int year = localDate.getYear();
			int monthValue = localDate.getMonthValue();
			String format = String.format("%02d", monthValue);
			map.put(year + "-" + format, 0);
			list.add(map);
		}
		return list;
	}

	//caculate betweenTime
	public static long betweenTime(Date beginTime,Date endTime) throws ParseException {

		long firstDateMilliSeconds = beginTime.getTime();
		long secondDateMilliSeconds = endTime.getTime();
		long subDateMilliSeconds =secondDateMilliSeconds - firstDateMilliSeconds;

		long result=subDateMilliSeconds/(24*60*60*1000);
		return result;
	}

}
